<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="title" v-model="visible" align-center :width="width" :z-index="2000" @close="handleClose">
    <component ref="formRef" :is="com" v-model="form" :options="options" />
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../../api'
import Type2 from './Type2'
import Type3 from './Type3'
import Type4 from './Type4'
import Type5 from './Type5'
import Type6 from './Type6'
import Type7 from './Type7'
import Type8 from './Type8'
import Type9 from './Type9'
import Type10 from './Type10'
import Type11 from './Type11'
import Type12 from './Type12'
import Type13 from './Type13'
import Type14 from './Type14'
import { ElMessage } from 'element-plus'
import XEUtils from 'xe-utils'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const type = ref()
const com = computed(() => ['', '', Type2, Type3, Type4, Type5, Type6, Type7, Type8, Type9, Type10, Type11, Type12, Type13, Type14][type.value || 0])
const title = computed(() => ['', '', '补货', '退款完结', '建单入库', '退货补货', '报废完结', '退货完结', '报废补货', '报废退款', '退货退款', '报损完结', '特采入库', '重新验货', '差异收货'][type.value || 0])
const width = computed(() => ['', '', '800px', '1200px', '800px', '800px', '500px', '800px', '800px', '1200px', '1200px', '800px', '800px', '500px', '800px'][type.value || 0])
const emit = defineEmits(['success', 'close'])

// 打开弹窗
const open = async (row, t) => {
  type.value = 0
  await 1
  type.value = t
  console.log('type.value', type.value)
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  const obj = XEUtils.clone(row, true)
  obj.process_num = obj.reject_num
  obj.process_type = t
  obj.unit_price = obj.is_drawback === 1 ? obj.unit_price : obj.unit_price_tax
  obj.refund_voucher = []
  form.value = obj
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const formRef = ref()
const submit = async () => {
  const validate = await formRef?.value.validate()
  if (!validate) return
  loadings.submit = true
  const { a_id, process_type } = form.value
  let process_content = form.value
  const { lc_id, logistics_number, except_deliver_time, process_num, sku } = form.value
  const logistics_name = props.options.logisticsOptions.find(d => d.value === lc_id)?.label
  if (type.value === 2) {
    process_content = { lc_id, logistics_name, logistics_number, except_deliver_time, process_num, sku }
  }
  if (type.value === 5) {
    process_content = {
      return: form.value,
      replenishment: { lc_id, logistics_name, logistics_number, except_deliver_time, process_num }
    }
  }
  if (type.value === 7) {
    process_content = {
      return: form.value,
    }
  }
  if (type.value === 8) {
    process_content = {
      scrap: { sku, process_num },
      replenishment: { lc_id, logistics_name, logistics_number, except_deliver_time, process_num }
    }
  }
  if (type.value === 9) {
    process_content = {
      scrap: { sku, process_num },
      refund: form.value
    }
  }
  if (type.value === 10) {
    process_content = {
      return: form.value,
      refund: form.value
    }
  }
  api.process({ a_id, process_type, process_content }).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>